package org.aeon.colorfulplanebattle.flying.entity;

import org.aeon.colorfulplanebattle.constant.ColorE;
import org.aeon.colorfulplanebattle.constant.DirectionE;
import org.aeon.colorfulplanebattle.flying.Utils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

import static org.aeon.colorfulplanebattle.constant.GameBasisConstant.*;

/**
 * @author: MissingAeon
 * @date: 2024/1/13 10:31
 * @description:
 */
public class EnemyShip extends Ship {

    private static final List<Map<String, BufferedImage>> pictures = new ArrayList<>();// 飞机图片集
    private static final Properties prop;

    // 加载飞机图片
    static {
        prop = Utils.getProp(ENEMY_SHIP);
        try {
            for (int i = 0; ; i++) {
                InputStream resource;
                HashMap<String, BufferedImage> map = new HashMap<>();
                for (ColorE colorE : ColorE.values()) {
                    resource = Ship.class.getClassLoader().getResourceAsStream("img/diji/diji" + i + "_" + colorE.name() + ".png");
                    if (resource == null) break;
                    map.put(colorE.name(), ImageIO.read(resource));
                }
                if (map.isEmpty()) break;
                pictures.add(map);
            }
        } catch (IOException ignored) {
        }
    }

    /**
     * 敌机创建使用
     */
    public EnemyShip() {
        super(ENEMY_CAMP);
        this.camp = ENEMY_CAMP;

        Utils.stuffing(prop, this);

        this.hp = this.maxHp;
        Random random = new Random();
        // 随机颜色
        color = ColorE.values()[random.nextInt(4)].name();
        // 随机图片
        picture = pictures.get(random.nextInt(pictures.size())).get(this.getColor());
        // 反转图像
        picture = Utils.flippedImage(picture);
    }

    @Override
    public boolean move(DirectionE direction) {
        switch (direction) {
            case UP -> y -= speed;
            case DOWN -> y += speed;
            case LEFT -> x -= speed;
            case RIGHT -> x += speed;
        }
        // 判断是否出边缘
        boolean widthEdge = x > BATTLEFIELD_WIDTH + width + speed * 10 || x < -width - speed * 10;
        boolean heightEdge = y > BATTLEFIELD_HEIGHT + height + speed * 10 || y < -height - speed * 10;
        return !(widthEdge || heightEdge);
    }

    @Override
    public void change() {

    }
}
